package com.marvels.approve.common.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.marvels.approve.common.service.IUploadFileService;
import com.marvels.approve.credit.dao.McamAttachmentInfoDao;
import com.marvels.approve.credit.dto.McamAttachmentInfoDTO;
import com.marvels.approve.process.dto.LaAttachAtmeTypeViewDTO;
import com.marvels.approve.process.service.AttachmentInfoService;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.MarvelsUtils;

@Service
public class UploadFileServiceImpl implements IUploadFileService {
	
	@Autowired
	private McamAttachmentInfoDao mcamAttachmentInfoDao;
	
	@Autowired
	private AttachmentInfoService attachmentInfoService;
	
	@Value("${marvels.file.upload.basepath}")
	private String fileBasePath;
	
	public Integer uploadFile(MultipartFile file, String loanId,List<String> supportType,McamAttachmentInfoDTO attachment) throws MarvelsBusinessException, IOException {
		InputStream uploadStream = null;
		if(file==null) throw new MarvelsBusinessException("上传的文件为空");
		try {
			uploadStream = file.getInputStream();
		} catch (Exception e) {
			throw new MarvelsBusinessException("获取上传的附件错误！");
		}
		String fileName = file.getOriginalFilename();
		String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
		if(!CollectionUtils.isEmpty(supportType) && !supportType.contains(suffix)){
			throw new MarvelsBusinessException("不支持"+suffix+"类型的文件上传！");
		}
		
		return saveFile(uploadStream,loanId,fileName,attachment);
	}

	private Integer saveFile(InputStream uploadStream, String loanId, String fileName, McamAttachmentInfoDTO attachment)
			throws MarvelsBusinessException, IOException {
		// 对文件的征信报告上传
		if (StringUtils.isEmpty(attachment.getAtmtType())) {
			attachment.setAtmtType("PDF_B");
		}
		if (StringUtils.isEmpty(attachment.getAtmtTypeName())) {
			attachment.setAtmtTypeName("征信报告");
		}
		attachment.setLoanId(loanId);

		String userName = MarvelsConstant.SYSTEM_USER;
		attachment.setCreatedBy(userName);
		attachment.setUpdatedBy(userName);

		String prefix = MarvelsUtils.getPathPrefix();
		if (StringUtils.isEmpty(attachment.getFileLocation())) {
			attachment.setFileLocation(fileBasePath + prefix + fileName);
		}
		attachment.setFileName(fileName);
		attachment.setUploadTime(new Date());
		attachment.setUploadUserId(userName);
		// 附件表数据新增
		Integer attachmentState = mcamAttachmentInfoDao.insertItem(attachment);
		if (attachmentState < 1) {
			throw new MarvelsBusinessException("征信报告储存失败!");
		}
		File directory = new File(fileBasePath + prefix);

		if (directory.exists() == false) {
			directory.mkdirs();
		}

		File creditFile = new File(attachment.getFileLocation());
		try {
			if (!creditFile.exists()) {
				creditFile.createNewFile();
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("创建文件异常", e);
			throw new MarvelsBusinessException("创建文件异常！");
		}
		MarvelsUtils.streamToFile(uploadStream, creditFile);
		return attachmentState;
	}
	
	@Override
	public List<LaAttachAtmeTypeViewDTO> getAttachmentsByViewKey(String viewType,
			String loanId) throws MarvelsBusinessException {
		LaAttachAtmeTypeViewDTO laAttachAtmeTypeView = new LaAttachAtmeTypeViewDTO();
		laAttachAtmeTypeView.setViewKey(viewType);
		List<LaAttachAtmeTypeViewDTO> mediaList = attachmentInfoService.getAttachmentsByViewKey(laAttachAtmeTypeView, loanId);
		return mediaList;
	}
}
